/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package keshe;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import javax.swing.JButton;
import javax.swing.JOptionPane;

public class UpdateStudent extends StudentEdit implements ActionListener{

    JButton b3=new JButton("修改");

    public UpdateStudent(){
        super();
        p12.add(b3);
        //对非必要文本框设置不可编辑
        s.setEnabled(false);
        name.setEditable(false);
        birth.setEditable(false);
        grade.setEnabled(false);
        clas.setEnabled(false);
        chinese.setEditable(false);
        english.setEditable(false);
        maths.setEditable(false);
        major.setEnabled(false);
        //注册监听器
        b1.addActionListener(this);
        b2.addActionListener(this);
        b3.addActionListener(this);
    }
    @Override
    public void actionPerformed(ActionEvent e) {
        // TODO 自动生成的方法存根
        if(e.getActionCommand()=="确定"){
            String number=num.getText();
            if(number.equals("")){
                JOptionPane.showMessageDialog(null, "不存在此学生", "Messages",
                        JOptionPane.INFORMATION_MESSAGE);
            }
            else
                try{
                    Connection con=DataBaseCreate.getConnection();
                    Statement sql=con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
                    ResultSet rs=sql.executeQuery("select * from student where Num like '%"+number+"%'");
                    //通过学号查找并显示数据到文本框
                    if(rs.next()){
                        s.setSelectedItem(rs.getString("Sex"));
                        name.setText(rs.getString("Name"));
                        birth.setText(rs.getString("Birth"));
                        chinese.setText(rs.getString("Chinese"));
                        english.setText(rs.getString("English"));
                        maths.setText(rs.getString("Maths"));
                        s.setSelectedItem(rs.getString("Sex"));
                        clas.setSelectedItem(rs.getString("Class"));
                        grade.setSelectedItem(rs.getString("Grade"));
                        major.setSelectedItem(rs.getString("Major"));
                        //必要文本框设为可编辑
                        num.setEditable(false);
                        s.setEnabled(true);
                        name.setEditable(true);
                        birth.setEditable(true);
                        grade.setEnabled(true);
                        clas.setEnabled(true);
                        chinese.setEditable(true);
                        english.setEditable(true);
                        maths.setEditable(true);
                        major.setEnabled(true);
                        b1.setEnabled(false);
                    }
                    else{
                        JOptionPane.showMessageDialog(null, "不存在此学生", "Messages",
                                JOptionPane.INFORMATION_MESSAGE);
                    }

                    sql.close();
                    con.close();

                }catch(java.lang.ClassNotFoundException err){
                    JOptionPane.showMessageDialog(null, err.getMessage(), "ClassNotFoundException:",
                            JOptionPane.INFORMATION_MESSAGE);
                }catch (SQLException ex){
                    JOptionPane.showMessageDialog(null, ex.getMessage(), "SQLException: ", JOptionPane.INFORMATION_MESSAGE);
                }
        }
        else if(e.getActionCommand()=="取消"){
            setVisible(false);
        }
        else if(e.getActionCommand()=="修改"){
            String number=num.getText();
            float total=Float.parseFloat(
                    chinese.getText())+Float.parseFloat(english.getText())+Float.parseFloat(maths.getText());

            try{
                Connection con=DataBaseCreate.getConnection();
                Statement sql=con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
                //通过学号查找数据
                ResultSet rs1=sql.executeQuery("select * from student where Num like '%"+number+"%'");
                if(rs1.next()){
                    //修改各字段数值
                    rs1.updateString("Name", name.getText());
                    rs1.updateString("Birth", birth.getText());
                    rs1.updateString("Sex", (String) s.getSelectedItem());
                    rs1.updateString("Class", (String) clas.getSelectedItem());
                    rs1.updateString("Grade", (String) grade.getSelectedItem());
                    rs1.updateString("Major", (String) major.getSelectedItem());
                    rs1.updateString("Chinese", chinese.getText());
                    rs1.updateString("English", english.getText());
                    rs1.updateString("Maths", maths.getText());
                    rs1.updateFloat("Total", total);
                    rs1.updateRow();
                }
                else{
                    JOptionPane.showMessageDialog(null, "不存在此学生", "Messages",
                            JOptionPane.INFORMATION_MESSAGE);
                }

                sql.close();
                con.close();
                JOptionPane.showMessageDialog(null, "成功修改学生信息与成绩", "Message",
                        JOptionPane.INFORMATION_MESSAGE);
                setVisible(false);

            }catch(java.lang.ClassNotFoundException err){
                JOptionPane.showMessageDialog(null, err.getMessage(), "ClassNotFoundException:",
                        JOptionPane.INFORMATION_MESSAGE);
            }catch (SQLException ex){
                JOptionPane.showMessageDialog(null, ex.getMessage(), "SQLException: ", JOptionPane.INFORMATION_MESSAGE);
            }

        }
    }
}

